package com.sunny.ynhz.model;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.CurrentRiverBean;
import com.sunny.ynhz.bean.WarpRiverBean;
import com.sunny.ynhz.ui.MainActivity;
import com.sunny.ynhz.utils.GsonUtils;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.ShowUtils;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/9/4.
 *
 * @describe TODO
 */

public class RiverMsgModel {
    Context context;
    IRiverModel iRiverModel;
    private List<CurrentRiverBean.DatecountBean> datecount;
    private ArrayList<CurrentRiverBean.DatecountBean> mList;
    private CurrentRiverBean mCurrentRiverBean;

    public interface IRiverModel<T> {
        void onSuccess(T t);

        void onFail();

        void onStartLoad();

    }

    public RiverMsgModel(Context context, IRiverModel iRiverModel) {
        this.context = context;
        this.iRiverModel = iRiverModel;

    }

    @SuppressWarnings("unchecked")
    public void getCurrentRiverData() {
        mCurrentRiverBean = GsonUtils.getInstance("riverId" + SPUtil.get("userId", "").toString(), CurrentRiverBean.class);
        if (mCurrentRiverBean != null && !(context instanceof MainActivity)) {
            Observable.create(new Observable.OnSubscribe<WarpRiverBean>() {
                @Override
                public void call(Subscriber<? super WarpRiverBean> subscriber) {
                    WarpRiverBean riverListMsg = getRiverListMsg(mCurrentRiverBean);
                    subscriber.onNext(riverListMsg);
                }
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<WarpRiverBean>() {
                @Override
                public void onCompleted() {
                    ShowUtils.dismissProgress();
                }

                @Override
                public void onError(Throwable e) {
                    ShowUtils.dismissProgress();
                }

                @Override
                public void onStart() {
                    super.onStart();
                    ShowUtils.disPlayProgress(context);
                }

                @Override
                public void onNext(WarpRiverBean warpRiverBean) {
                    if (iRiverModel != null) {
                        iRiverModel.onSuccess(warpRiverBean);
                    }
                }
            });

            return;
        }
        HttpManager.getInstance().getRiverSystemBean(SPUtil.get("userId", "").toString()).map(currentRiverBean -> {
            WarpRiverBean riverBean = null;
            mCurrentRiverBean = currentRiverBean;
            GsonUtils.putString("riverId" + SPUtil.get("userId", "").toString(), currentRiverBean);
            if (iRiverModel != null) {
                riverBean = getRiverListMsg(currentRiverBean);
            }
            return riverBean;
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<WarpRiverBean>() {
            @Override
            public void onNext(WarpRiverBean currentRiverBean) {
                if (iRiverModel != null) {
                    iRiverModel.onSuccess(currentRiverBean);
                }
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (iRiverModel != null) {
                    iRiverModel.onFail();
                }
            }

            @Override
            public void onstart() {
                if (iRiverModel != null) {
                    iRiverModel.onStartLoad();
                }
            }
        }));
    }







    private WarpRiverBean getRiverListMsg(CurrentRiverBean currentRiverBean) {
        WarpRiverBean warpRiverBean = new WarpRiverBean();
        mList = new ArrayList<>();
        datecount = currentRiverBean.getDatecount();
        for (int i = 0; i < datecount.size(); i++) {
            CurrentRiverBean.DatecountBean datecountBean = datecount.get(i);
            String parentid = datecountBean.getPARENTID();
            if (parentid.equals("0")) {
                mList.add(datecountBean);
            } else {
                String parentids = datecountBean.getPARENTIDS();
                if (parentids.endsWith(",")) {
                    parentids = parentids.substring(0, parentids.length());
                }
                String[] split = parentids.split(",");
                boolean isBreak = false;
                for (int j = 0; j < split.length; j++) {
                    if (isBreak) {
                        break;
                    }
                    String id = split[j];
                    for (int k = 0; k < datecount.size(); k++) {
                        CurrentRiverBean.DatecountBean datecountBean1 = datecount.get(k);
                        if (datecountBean1.getID().equals(id) && adjustIsTop(datecount, datecountBean1) && !datecountBean1.getPARENTID().equals("0")) {
                            isBreak = true;
                            if (!mList.contains(datecountBean1)) {
                                mList.add(datecountBean1);
                            }
                            break;
                        }
                    }
                }
            }
        }
        List<Object> riverList = new ArrayList<>();
        riverList.addAll(datecount);
        riverList.addAll(currentRiverBean.getData());
        warpRiverBean.setMdateCountBeans(mList);
        warpRiverBean.setmDataBeans(currentRiverBean.getData());
        warpRiverBean.setmList(riverList);
        return warpRiverBean;

    }

    public boolean adjustIsTop(List<CurrentRiverBean.DatecountBean> datecount, CurrentRiverBean.DatecountBean datecountBean1) {
        if (datecountBean1.getPARENTID().equals("0")) {
            return true;
        } else {
            for (int i = 0; i < datecount.size(); i++) {
                CurrentRiverBean.DatecountBean datecountBean = datecount.get(i);
                String parentid = datecountBean1.getPARENTID();
                if (datecountBean.getID().equals(parentid)) {
                    return false;
                }
            }
            return true;
        }
    }


    public void getCurrentSingleDataCountBeans(String id, List<CurrentRiverBean.DatecountBean> mList) {
        List<CurrentRiverBean.DatecountBean> datecount = mCurrentRiverBean.getDatecount();
        for (int i = 0; i < datecount.size(); i++) {
            CurrentRiverBean.DatecountBean datecountBean = datecount.get(i);
            if (datecountBean.getPARENTID().equals(id)) {
                mList.add(datecountBean);
                getCurrentSingleDataCountBeans(datecountBean.getID(), mList);
            }
        }
    }

    public List<CurrentRiverBean.DatecountBean> getCurrentSingleDataCountsBean(String id) {
        List<CurrentRiverBean.DatecountBean> mList = new ArrayList<>();
        getCurrentSingleDataCountBeans(id, mList);
        return mList;
    }


}
